//
//  GuideViewController.m
//  GuideMe
//
//  Created by Amila Fonseka on 3/24/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "GuideViewController.h"
#import "PlaceDetailsViewControllers.h"
#import "GuideCell.h"

@implementation GuideViewController

@synthesize strCommingFrom;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        arrGuide = [[NSMutableArray alloc] initWithArray:[NSArray arrayWithObjects:
                                                          NSLocalizedString(@"Head toward Main Street southwest on Sea Street sdjhas kjdasd (95 m)", @""),
                                                          NSLocalizedString(@"Continue onto Galle Road (57 m)", @""),
                                                          NSLocalizedString(@"Slight left onto Custom Rd (280 m)", @""),
                                                          NSLocalizedString(@"At the roundabout, take the 1st exit onto Light House St. Pass by Fort New Entrance (on the left) (110 m)", @""),
                                                          NSLocalizedString(@"Slight left onto Church St. Pass by St.Joseph's College (on the right) (260 m)", @""),
                                                          NSLocalizedString(@"Now @ Fort Galle", @""),
                                                          NSLocalizedString(@"Head toward Middle St. northwest on Church St. Pass by St.Joseph's College (on the left) (260 m)", @""),
                                                          NSLocalizedString(@"Slight right onto Light House St. Pass by Fort New Entrance . (on the right) (86 m)", @""),
                                                          NSLocalizedString(@"At the roundabout, take the 2nd exit onto Custom Rd (350 m)", @""),
                                                          NSLocalizedString(@"Continue onto Galle Road (57 m)", @""),
                                                          NSLocalizedString(@"Slight right onto Sea Street. Pass by HSBC . (on the left in 750 m) (1.2 km)", @""),
                                                          NSLocalizedString(@"Sea Street turns slightly right and becomesGalle Road. Pass by LAUGFS. (1.3 km)", @""),
                                                          NSLocalizedString(@"At Niyas Arts, continue onto Matara Road/A2. Continue to follow A2. Pass by Dewata Thakkiya (on the left) (2.4 km)", @""),
                                                          NSLocalizedString(@"Now @ Unawatuna", @""),
                                                          NSLocalizedString(@"Head toward Wallaye Deywala Rd. south on Matara Road. Pass by Kings Palm Villa .(on the left) (9.1 km)", @""),
                                                          NSLocalizedString(@"Turn left (350 m)", @""),
                                                          NSLocalizedString(@"Turn left (300 m)", @""),
                                                          NSLocalizedString(@"Turn right (210 m)", @""),
                                                          NSLocalizedString(@"Now @ Koggala", @""),
                                                          NSLocalizedString(@"Head northwest (200 m)", @""),
                                                          NSLocalizedString(@"Turn left. toward Matara Road	(350 m)", @""),
                                                          NSLocalizedString(@"Turn left onto Matara Road", @""),
                                                          NSLocalizedString(@"Pass by South Point Villa -School for the Children of the Community (on the left in 2.0 km) (5.9 km) Turn left onto Dharmarama Mawatha (750 m)", @""),
                                                          NSLocalizedString(@"7.2 km – about 10 mins.", @""),
                                                          NSLocalizedString(@"Now @ Mahaviharaya Ahangama", @""),
                                                          NSLocalizedString(@"Head northwest (200 m)", @""),
                                                          NSLocalizedString(@"Turn left. toward Matara Road/A2 (350 m)", @""),
                                                          NSLocalizedString(@"Turn left onto Matara Road/A2.Continue to follow A2.Pass by South Point Villa -School for the Children of the Community . (on the left in 2.0 km)	(8.4 km)", @""),
                                                          NSLocalizedString(@"Keep left at the fork (3.8 km)", @""),
                                                          NSLocalizedString(@"Turn left to stay on Matara Road.Pass by Bodhirukkaramaya - Walgama. (on the left in 13.2 km) (15.1 km)", @""),
                                                          NSLocalizedString(@"Slight right onto Main St. Pass by Pamburana Filling Station . (on the left) (2.0 km)", @""),
                                                          NSLocalizedString(@"Turn right at Fire Brigadge. onto Colombo - Galle - Hambantota - Wellawaya Hwy", @""),
                                                          NSLocalizedString(@"Pass by Munisipal Council (on the right) (400 m)", @""),
                                                          NSLocalizedString(@"Turn right onto Tangalle Road (33 m)", @""),
                                                          NSLocalizedString(@"At the roundabout, take the 1st exit. onto Buddhika P. Senevirathne (300 m)", @""),
                                                          NSLocalizedString(@"Slight left at Provincial Revenue Dept.onto Kachcheri Rd.Pass by District Secretariat .(on the left) (280 m)", @""),
                                                          NSLocalizedString(@"Turn right onto Buddhika P. Senevirathne (56 m)", @""),
                                                          NSLocalizedString(@"Slight left to stay on Buddhika P. Senevirathne (130 m)", @""),
                                                          NSLocalizedString(@"Slight left (53 m)", @""),
                                                          NSLocalizedString(@"Now @ Fort Matara", @""),
                                                          NSLocalizedString(@"Head toward Buddhika P. Senevirathne. north on Unknown road. 	(53 m)", @""),
                                                          NSLocalizedString(@"Slight right onto Buddhika P. Senevirathne (190 m)", @""),
                                                          NSLocalizedString(@"Take the 3rd left. onto Kachcheri Rd. Pass by District Secretariat (on the right) (280 m)", @""),
                                                          NSLocalizedString(@"Provincial Revenue Dept, continue onto Buddhika P. Senevirathne (260 m)", @""),
                                                          NSLocalizedString(@"At the roundabout, take the 1st exit. onto Tangalle Road. 	(80 m)", @""),
                                                          NSLocalizedString(@"Turn right onto B338. Pass by Sarasi Flora . (on the right)  (900 m)", @""),
                                                          NSLocalizedString(@"Continue onto Tangalle Road/A2. Continue to follow A2. Pass by Pahala Peekwella Temple (on the right) .Destination will be on the right.   (4.7 km) ", @""),
                                                          NSLocalizedString(@"Now @ Devinuwara Dewalaya", @""),
                                                          NSLocalizedString(@"Head toward Galgane Road. east on Tangalle Road/A2. Continue to follow A2. Pass by Fuel Station (on the left in 550 m)  (28.3 km)", @""),
                                                          NSLocalizedString(@"Turn left (290 m)", @""),
                                                          NSLocalizedString(@"Now @ Tangalla", @""),
                                                          nil]];
        
        
       
        
        
        arrPlaces = [[NSMutableArray alloc] initWithArray:[NSArray arrayWithObjects:
                                                          NSLocalizedString(@"Galle", @""),
                                                          NSLocalizedString(@"Dutch Fort", @""),
                                                          NSLocalizedString(@"Unawatuna", @""),
                                                          NSLocalizedString(@"Koggala", @""),
                                                          NSLocalizedString(@"Martin Wickramasinghe Memorial Museum", @""),
                                                          NSLocalizedString(@"Ahangama / Midigama", @""),
                                                          NSLocalizedString(@"Matara", @""),
                                                          NSLocalizedString(@"Matara Fort", @""),
                                                          NSLocalizedString(@"Dondra / Devinuwara", @""),
                                                          NSLocalizedString(@"Tangalla", @""),
                                                          nil]];
    }
    return self;
}

- (IBAction)btnBackTapped
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tabelView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (selectedSegmentIndex == 0)
    {
        NSString *strTempMsg = [arrGuide objectAtIndex:indexPath.row];
		
		CGSize maximumLabelSizeForMessage = CGSizeMake(280.0, 100.0);
		CGSize expectedLabelSizeForMessage = [strTempMsg sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14]
													constrainedToSize:maximumLabelSizeForMessage 
														lineBreakMode:UILineBreakModeWordWrap];
		
		CGFloat cellHeight = expectedLabelSizeForMessage.height + 20.0;
		
		if (cellHeight < 60)
		{
			cellHeight = 60.0;
		}
		
		//NSLog(@"Cell Height : %f", cellHeight);
		
		return cellHeight;
    }
    else if (selectedSegmentIndex == 1)
    {
        return 50.0;
    }
    
	return 40.0;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	if(section==0)
    {
        if (selectedSegmentIndex == 0)
        {
            if ([arrGuide count] > 0)
            {
                return [arrGuide count];
            }
            return 0;
        }
        else if (selectedSegmentIndex == 1)
        {
            if ([arrPlaces count] > 0)
            {
                return [arrPlaces count];
            }
            return 0;
        }
        
        return 0;
        //            if ([arrPlaces count] > 0)
        //                return [arrPlaces count];
        //            return 0;
    }
	
	return 0;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{	
	static NSString *EmptyCellIdentifier = @"EmptyCell";
	
	UITableViewCell *emptyCell = [tableView dequeueReusableCellWithIdentifier:EmptyCellIdentifier];
	if (emptyCell == nil)
	{
		emptyCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:EmptyCellIdentifier] autorelease];   
		emptyCell.detailTextLabel.textAlignment = UITextAlignmentLeft;
		emptyCell.selectionStyle = UITableViewCellSelectionStyleNone;
	}
    
    static NSString *GuideCellIdentifier = @"GuideCell";		
	
	GuideCell *gCell = (GuideCell *)[tableView dequeueReusableCellWithIdentifier:GuideCellIdentifier];
	if (gCell == nil)
	{
		gCell = [[[GuideCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:GuideCellIdentifier] autorelease];
		gCell.selectionStyle = UITableViewCellSelectionStyleNone;
	}
    
	if (selectedSegmentIndex == 0)
    {
        if ([arrGuide count] > 0)
        {
//            NSString *strTemp = [arrGuide objectAtIndex:indexPath.row];
//            strTemp = [strTemp stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//            
//            CGSize maximumLabelSizeForUsername = CGSizeMake(280.0, 25.0);
//            CGSize expectedLabelSizeForUsername = [strTemp sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14]
//                                                              constrainedToSize:maximumLabelSizeForUsername 
//                                                                  lineBreakMode:UILineBreakModeWordWrap];
//            
//            
//            CGRect frameForUsername=gCell.lblTourLocation.frame;
//            frameForUsername.size.width = expectedLabelSizeForUsername.width;
//            frameForUsername.size.height = expectedLabelSizeForUsername.height;
//            gCell.lblTourLocation.frame = frameForUsername;
            
            //emptyCell.detailTextLabel.frame = CGRectMake(20.0, 10.0, 280.0, 40.0);
            //emptyCell.contentView.backgroundColor = [UIColor whiteColor];
            
            NSString *strTemp = [arrGuide objectAtIndex:indexPath.row];
            CGSize maximumLabelSizeForMessage = CGSizeMake(280.0, 100.0);
            CGSize expectedLabelSizeForMessage = [strTemp sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14]
                                                        constrainedToSize:maximumLabelSizeForMessage 
                                                            lineBreakMode:UILineBreakModeWordWrap];
            
            CGFloat cellTop = 10.0;
            CGFloat cellHeight = expectedLabelSizeForMessage.height + 20.0;
            
            if (cellHeight < 60)
            {
                cellHeight = 60.0;
            }
            
            //CGFloat msgCellYPosition = ((cellHeight - expectedLabelSizeForMessage.height)/2);
            CGFloat msgCellYPosition = cellTop + ((cellHeight - expectedLabelSizeForMessage.height - 10)/2 );
            
            //NSLog(@"Cell Y Position : %f", msgCellYPosition);
            //NSLog(@"Cell Height : %f", expectedLabelSizeForMessage.height);
            
            CGRect msgFrame = CGRectMake(20, (int) msgCellYPosition , 280, expectedLabelSizeForMessage.height);
            
            gCell.lblTourLocation.frame = msgFrame;
            
            if ([strTemp hasPrefix:@"Now @"])
            {
                gCell.lblTourLocation.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
                //NSLog(@"TRUE");
                gCell.lblTourLocation.text = [NSString stringWithString:[[arrGuide objectAtIndex:indexPath.row] stringByReplacingOccurrencesOfString:@"*" withString:@""]];
                
            }
            else
            {
                gCell.lblTourLocation.font = [UIFont fontWithName:@"Helvitica" size:14];
                gCell.lblTourLocation.text = strTemp;
            }
            
            gCell.lblTourLocation.text = [NSString stringWithFormat:@"%@", [arrGuide objectAtIndex:indexPath.row]];
            return gCell;
        }
        else
        {
            emptyCell.detailTextLabel.frame = CGRectMake(20.0, 10.0, 280.0, 30.0);
            emptyCell.contentView.backgroundColor = [UIColor whiteColor];
            emptyCell.detailTextLabel.text = @"Sorry there are no guides at the moment";
            return emptyCell;
        }
    }
    else if (selectedSegmentIndex == 1)
    {
        if ([arrPlaces count] > 0)
        {
            emptyCell.detailTextLabel.frame = CGRectMake(20.0, 10.0, 280.0, 30.0);
            emptyCell.contentView.backgroundColor = [UIColor whiteColor];
            emptyCell.detailTextLabel.textColor = [UIColor blackColor];
            emptyCell.detailTextLabel.text = [NSString stringWithFormat:@"%@", [arrPlaces objectAtIndex:indexPath.row]];
            return emptyCell;
        }
        else
        {
            emptyCell.detailTextLabel.frame = CGRectMake(20.0, 10.0, 280.0, 30.0);
            emptyCell.contentView.backgroundColor = [UIColor whiteColor];
            emptyCell.detailTextLabel.text = @"Sorry there are no places at the moment";
            return emptyCell;
        }
    }
    else
    {
        emptyCell.detailTextLabel.frame = CGRectMake(20.0, 10.0, 280.0, 30.0);
        emptyCell.contentView.backgroundColor = [UIColor whiteColor];
        emptyCell.detailTextLabel.text = @"Sorry there are no guides or places at the moment";
        return emptyCell;
    }
    return emptyCell;	
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (selectedSegmentIndex == 0)
    {
        
    }
    
    if (selectedSegmentIndex == 1)
    {
        if ([arrPlaces count] > 0)
        {
            PlaceDetailsViewControllers *tourView = [[PlaceDetailsViewControllers alloc] initWithNibName:@"PlaceDetailsViewControllers" bundle:nil];
            tourView.strPlaceID = [NSString stringWithFormat:@"%d", (indexPath.row+1)];
            tourView.strPlaceTitle = [arrPlaces objectAtIndex:indexPath.row];
            [self.navigationController pushViewController:tourView animated:YES];
            [tblList deselectRowAtIndexPath:indexPath animated:YES];
            [tourView release];
        }
    }
}



- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    NSLog(@"Selected Tab : %@", item.title);
    
    if ([item.title isEqualToString:@"Guide"])
    {
        lblTitle.text = @"Guide";
        selectedSegmentIndex = 0;
    }
    else if ([item.title isEqualToString:@"Places"])
    {
        lblTitle.text = @"Places";
        selectedSegmentIndex = 1;
    }
    
    tabBar.selectedItem = item;
    [tblList reloadData];
}




#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    if ([strCommingFrom isEqualToString:@"Guide"])
    {
        lblTitle.text = @"Guide";
        selectedSegmentIndex = 0;
        [tabList setSelectedItem:tabBarGuide];
    }
    else if ([strCommingFrom isEqualToString:@"Places"])
    {
        lblTitle.text = @"Places";
        selectedSegmentIndex = 1;
        [tabList setSelectedItem:tabBarPlace];
    }
    
    [tblList reloadData];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
